/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms.Design;
using EnvDTE;
using DaveSexton.DocProject.Configuration;
using DaveSexton.DocProject.Sandcastle.Configuration;
using DaveSexton.DocProject.Sandcastle.TopicManagement;

namespace DaveSexton.DocProject.Sandcastle
{
	internal sealed class Help2xInvokerToolBarCommand : ToolBarCommand
	{
		#region Public Properties
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1802:UseLiteralsWhereAppropriate")]
		public static readonly string DefaultName = "Help2xInvoker";

		public override string Name
		{
			get { return DefaultName; }
		}

		public override string Tooltip
		{
			get
			{
				return Resources.Text.Help2xInvokerCommandTooltip;
			}
		}

		public override bool UseOfficeBitmapResourceId
		{
			get { return false; }
		}

		public override System.Drawing.Image Image
		{
			get
			{
				return Resources.Images.Help2x;
			}
		}

		public override System.Drawing.Color ImageTransparencyKey
		{
			get
			{
				return System.Drawing.Color.Fuchsia;
			}
		}

		public override vsCommandStatus Status
		{
			get
			{
				if (CompiledHelp2xFile != null)
					return vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
				else
					return vsCommandStatus.vsCommandStatusSupported;
			}
		}
		#endregion

		#region Private / Protected
		private static string CompiledHelp2xFile
		{
			get
			{
				IDocProject project = DocProjectEnvironment.SelectedProject;

				// NOTE: This property is used to determine the status of this tool bar button.
				// The state of tool bar commands is refreshed often in Visual Studio, even while the New Project Wizard is running.
				// If the selected DocProject is being created and the wizard is still open, then this property must not access the engine's
				// settings or options.  These objects are delay-loaded and they rely on an associated presentation, which hasn't been 
				// assigned to the project yet since it's still being configured by the user.  The IsProjectValid method ensures that the 
				// presentation name has been assigned, so it must be called here for verification before this command can be enabled for
				// the selected project.
				if (project != null && typeof(SandcastleBuildEngineProvider).IsAssignableFrom(project.Provider.GetType())
					&& SandcastleBuildEngineProvider.IsProjectValid(project))
				{
					SandcastleBuildEngine engine = (SandcastleBuildEngine) BuildController.GetEngineForProject(project);
					string file = engine.Settings.CompiledHelp2xFile;

					if (!string.IsNullOrEmpty(file) && System.IO.File.Exists(file))
						return file;
				}

				return null;
			}
		}
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="Help2xInvokerToolBarCommand" /> class.
		/// </summary>
		public Help2xInvokerToolBarCommand()
		{
		}
		#endregion

		#region Methods
		public override string GetStatusText(object status)
		{
			return Resources.Text.Help2xInvokerCommandText;
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		public override object Execute(object option, object argument)
		{
			string file = CompiledHelp2xFile;

			if (file == null)
				return null;
			else
			{
				try
				{
					System.Diagnostics.Process.Start(file);
					return file;
				}
				catch (Exception ex)
				{
					DocProjectEnvironment.ShowErrorDialog(ex.Message);
				}

				return null;
			}
		}
		#endregion
	}
}